package com.nantong.admin.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;

import com.nantong.admin.domain.Product;
import com.nantong.admin.domain.model.request.product.ProductSearch;
import com.nantong.admin.domain.model.response.product.ProductVo;
import com.nantong.admin.mapper.ProductMapper;
import com.nantong.admin.service.IProductService;
import com.nantong.common.core.utils.StringUtils;
import com.nantong.common.mybatis.core.page.PageQuery;
import com.nantong.common.mybatis.core.page.TableDataInfo;

import org.springframework.stereotype.Service;

/**
 * 产品Service实现类
 */
@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    private final ProductMapper productMapper;

    @Override
    public TableDataInfo<ProductVo> selectPageList(ProductSearch productSearch, PageQuery pageQuery) {
        Page<ProductVo> page = productMapper.selectPageList(pageQuery.build(), buildQueryWrapper(productSearch));
        return TableDataInfo.build(page);
    }

    @Override
    public List<ProductVo> searchList(ProductSearch productSearch) {
        Wrapper<Product> productWrapper = this.buildQueryWrapper(productSearch);
        return productMapper.selectVoList(productWrapper);
    }

    @Override
    public Product selectProductById(Long productId) {
        return this.getById(productId);
    }

    @Override
    public int insertProduct(Product product) {
        return this.save(product) ? 1 : 0;
    }

    @Override
    public int updateProduct(Product product) {
        return this.updateById(product) ? 1 : 0;
    }

    @Override
    public int deleteProductById(Long productId) {
        return this.removeById(productId) ? 1 : 0;
    }

    private Wrapper<Product> buildQueryWrapper(ProductSearch productSearch) {
        Map<String, Object> params = productSearch.getParams();
        LambdaQueryWrapper<Product> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(productSearch.getProductName()), Product::getProductName,
                        productSearch.getProductName())
                .like(StringUtils.isNotBlank(productSearch.getInspectionStandard()), Product::getInspectionStandard,
                        productSearch.getInspectionStandard())
                .eq(ObjectUtil.isNotNull(productSearch.getProductType()), Product::getProductType, productSearch.getProductType())
                .between(params.get("beginTime") != null && params.get("endTime") != null,
                        Product::getCreateTime, params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}